package org.spring.springboot.app.community.housecost.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.housecost.dao.CommunityHouseCostDao;
import org.spring.springboot.app.community.housecost.domain.po.CommunityHouseCostPO;
import org.spring.springboot.app.community.housecost.domain.vo.CommunityHouseCostVO;
import org.spring.springboot.app.community.housecost.domain.vo.HouseCostSearchVO;
import org.spring.springboot.app.community.housecost.domain.vo.TaskHouseCostVO;
import org.spring.springboot.app.community.housecost.domain.vo.UpdateHouseCostVO;
import org.spring.springboot.app.community.housecost.service.ICommunityHouseCostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommunityHouseCostServiceImpl implements ICommunityHouseCostService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommunityHouseCostServiceImpl.class);

    @Autowired
    private CommunityHouseCostDao communityHouseCostDao;

    @Override
    public List<CommunityHouseCostVO> findAll(HouseCostSearchVO searchVo,int pageNum,int pageSize)
    {
        if(StringUtils.isNotBlank(searchVo.getOfficeId())&&searchVo.getOfficeId().equals("0")){
            CommunityHouseCostPO  po= new CommunityHouseCostPO();
            searchVo.setOfficeId(po.getUser().getOfficeId());
        }
        return communityHouseCostDao.findAll(searchVo, pageNum, pageSize);
    }
    @Override
    public int delete(String id) {
        CommunityHouseCostPO  c= new CommunityHouseCostPO();
        c.setId(id);
        c.preDelete();
        return communityHouseCostDao.updateByPrimaryKeySelective(c);
    }

    @Override
    public R update(UpdateHouseCostVO vo){
        R r = new R();
        CommunityHouseCostPO  po= new CommunityHouseCostPO();
        BeanUtils.copyProperties(vo, po);
        if(StringUtils.isBlank(po.getId())){
            return r.fail().setMessage("ID不能为空");
        }
        if(communityHouseCostDao.updateByPrimaryKeySelective(po)<1){
            return r.fail().setMessage("没有一条数据被更新！");
        }
        //todo 需要更新房间表的到期时间
        return   r.success();
    }

        //一天一个

    /**
     *  生成物管费
     * @param vo
     * @return
     */
    @Override
    public R createHouserCost(TaskHouseCostVO vo){
        R r = new R();
        List<CommunityHouseCostPO> list= communityHouseCostDao.selectHouseCostByCreate(vo);
        Integer i=0;
        while (list != null && !list.isEmpty()) {
            for (CommunityHouseCostPO po : list){
                po.preInsert();
                po.setHasGive("2");
                communityHouseCostDao.insertSelective(po);
                i++;
            }
            list = communityHouseCostDao.selectHouseCostByCreate(vo);
        }
        r.setData(i);
        return   r.success();
    }

    /**
     *
     * @param houseId
     * @return
     */
    @Override
    public R replaceHouseExpireDay(String houseId){
        R r = new R();
        if(communityHouseCostDao.replaceHouseExpireDay(houseId)<1){
            return r.success().setMessage("没有数据更新");
        }
        return   r.success();
    }
}
